package com.hx.localmiaosha.web;

import com.hx.common.R;
import com.hx.localmiaosha.service.MiaoShaCheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author wonghiu
 * @Date 2021/6/4 23:55
 * @Version 1.0.1
 *
 * // 用于压测对比的WEB入口
 */
@Controller
@RequestMapping("/miaoshaTest")
@Slf4j
public class MiaoShaCheckController {
    @Autowired
    private MiaoShaCheckService miaoShaService;

    @PostMapping(value = "/killNoCacheLocal")
    @ResponseBody
    /***
     * 不预库存、不使用消息队列
     * 直接远程调用服务完成秒杀
     */
    public R killNoCache(@RequestParam("goodsId") Long goodsId){
        log.info(goodsId.toString());
        // 校验,并获取分布式订单号
        Long orderId = miaoShaService.killForTestNoCacheLocal(goodsId);

        if (orderId==null){
            return R.error("秒杀失败");
        }
        return R.ok("秒杀成功");
    }

    @PostMapping(value = "/killWithCacheLocal")
    @ResponseBody
    /***
     * 使用redis缓存，预库存
     */
    public R killWithCache(@RequestParam("goodsId") Long goodsId){
        log.info(goodsId.toString());
        // 校验,并获取分布式订单号
        Long orderId = miaoShaService.killForTestWithCacheLocal(goodsId);
        if (orderId==null){
            return R.error("秒杀失败");
        }
        return R.ok("秒杀成功");
    }

    @PostMapping(value = "/killNoCacheNoSyncLocal")
    @ResponseBody
    public R illForNoCacheNoSync(@RequestParam("goodsId") Long goodsId){
        log.info(goodsId.toString());
        // 校验,并获取分布式订单号
        Long orderId = miaoShaService.killForTestNoCacheNoSyncLocal(goodsId);
        if (orderId==null){
            return R.error("秒杀失败");
        }
        return R.ok("秒杀成功");
    }

    @PostMapping(value = "/killWithCacheNoSyncLocal")
    @ResponseBody
    /***
     * 使用redis缓存，预库存
     */
    public R illForWithCacheNoSync(@RequestParam("goodsId") Long goodsId){
        log.info(goodsId.toString());
        // 校验,并获取分布式订单号
        Long orderId = miaoShaService.killForTestWithCacheNoSyncLocal(goodsId);
        if (orderId==null){
            return R.error("秒杀失败");
        }
        return R.ok("秒杀成功");
    }
}
